package kku.cs.fgl;

import java.awt.Point;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.geom.Shape;

public class IsometricViewPort extends TilemapViewPort {
    private float start_x,start_y;

	public IsometricViewPort(int id, AbstractScene scene, float x, float y) {
		super(id, scene, x, y);
		start_x = 0;
		setTileWidth(32);
		setTileHeight(16);
	}

	@Override
	public Shape getTileShape(int col, int row) {
		float p[]=new float[8];
		Point t = getTileLocation(col,row);
		int tw=getTileWidth();
		int th=getTileHeight();
		p[0]=t.x;
		p[1]=t.y;
		p[2]=t.x+tw/2;
		p[3]=t.y-th/2;
		p[4]=t.x+tw;
		p[5]=t.y;
		p[6]=t.x+tw/2;
		p[7]=t.y+th/2;		
		return new Polygon(p);
	}

	public Point getTileLocation(int col,int row) {
		Point p=new  Point();
		int tw=getTileWidth();
		int th=getTileHeight();
		start_y = th*(getMapheight()+getMapwidth())/2;		
		p.x = (int)(start_x+(tw/2)*(col+row) );
		p.y = (int)(start_y-(th/2)*(col-row) );		
		return p;
	}

	@Override
	public void setTileHeight(int tileHeight) {
		setTileSize(tileHeight*2,tileHeight);
	}
	@Override
	public void setTileWidth(int tileWidth) {
		setTileSize(tileWidth,tileWidth/2);
	}
	@Override
	public void setTileSize(int tw, int th) {
		super.setTileSize(tw, tw/2);
	}
	public Point getCellFromXY(float x,float y) {
		int tw=getTileWidth();
		int th=getTileHeight();
		float mx,my;
//		start_y = th*getMapheight()*getMapwidth()/4;		
		mx = x-start_x;
		my = y-start_y;
		float px=((mx - (2*my))/(tw));
		float py =((my*2)/th + px);	
		return new Point((int)px,(int)py);
	}	
	/**
	 * Render SpriteCell
	 * 
	 * @param g
	 * @param tx
	 *            :tile x
	 * @param ty
	 *            :tile y
	 * @param x
	 *            :location to render
	 * @param y
	 *            :location to render
	 */
	public void paintCell(Graphics g, int tx, int ty, Shape r, SpriteCell cell) {
		if (cell != null) {
			int h=cell.getHeight();
			cell.paint(g, (int) r.getX(), (int) r.getMaxY()-h, getBgColor());
		}
	}
	
}
